#ifndef _PROJECTZ_NUSER_NCOMMAND_CS_PK_RESULT_H_
#define _PROJECTZ_NUSER_NCOMMAND_CS_PK_RESULT_H_

#include "../../Command.h"

#include "../../engine.h"

#include "../../User.h"
#include "../../Proxy.h"

#include "../../GameLog.h"
#include "../../NResource/Static.h"
#include "../../NLogic/Battle.h"
#include "../../NLogic/Static.h"
#include "../../NLogic/Function.h"
#include "../../NLogic/DailyMission.h"


namespace ProjectZ { namespace NCommand {

void Command::CS_PK_RESULT(User * pUser, ::Network::Packet * pPacket)
{
	STRESSLOG("%s", __FUNCTION__);

	PREPARE_PACKET;

	NLogic::Space * pSpace = pUser->GetSpace();
	if ( pSpace == NULL )
	{
		NLogic::NFunction::SendExceptionNoSpace(pUser);
		DEBUGLOG("%s pSpace == NULL", __FUNCTION__);
		return;
	}
	NLogic::Party * pParty = pUser->GetParty();
	if ( pParty == NULL )
	{
		NLogic::NFunction::SendExceptionNoParty(pUser);
		DEBUGLOG("%s pParty == NULL", __FUNCTION__);
		return;
	}

	NLogic::Battle * pBattleRoyalBattle = pUser->GetBattleRoyalBattle();
	if (NULL == pBattleRoyalBattle)
	{
		return;
	}

	if (true == pBattleRoyalBattle->GetData()._pkReward)
	{
		DEBUGLOG("CS_GET_MATCH_RESULT true == pUser->GetBattle()->GetData()._pvpReward");
		return;
	}

	time_t curtime = time(NULL);
	if( NLogic::NFunction::GetDailyInitTime(curtime) > static_cast<time_t>(pUser->GetCharacterInfo().drill_start) )
	{
		pUser->GetCharacterInfo().daily_ep = 0;
		pUser->GetCharacterInfo().drill_start = curtime;
	}

	int my_rank		= ::ProjectZ::NLogic::NFunction::GetPKSessionRank(pUser->GetUserSeq(), pSpace);//pParty, pUser->GetUserSeq());
	int my_point = NResource::Static::InstancePtr()->GetPointResource()->PKEvolvePoint(pUser, my_rank);
	pUser->GetCharacterInfo().daily_ep += my_point;

	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	*pAck << static_cast<uint8_t>(my_rank);
	*pAck << static_cast<uint8_t>(pSpace->GetBattleTree().size());


	auto itr = pSpace->GetBattleTree().begin();
	for ( ; itr != pSpace->GetBattleTree().end() ; ++itr )
	{
		int rank = 0;
		int ep_this_game = 0;
		uint32_t opp_ep_current = 0;
		NLogic::Battle * pBattle = itr->second;

		rank = ::ProjectZ::NLogic::NFunction::GetPKSessionRank(pBattle->GetData().userseq, pSpace);

		User * localUser = pBattle->GetUser();
		if( NULL != localUser )
		{
			ep_this_game = NResource::Static::InstancePtr()->GetPointResource()->PKEvolvePoint(localUser, rank);

			opp_ep_current = localUser->GetCharacterInfo().ep_current + ep_this_game;
			if( opp_ep_current > localUser->GetCharacterInfo().ep_max )
				opp_ep_current = localUser->GetCharacterInfo().ep_max;
			else if( opp_ep_current < 0 )
				opp_ep_current = 0;
		}

		DEBUGLOG("%s userseq=%d nickname=%s rank=%d",__FUNCTION__, pBattle->GetData().userseq, pBattle->GetData().nickname.c_str(), rank);
		*pAck << static_cast<uint8_t>(rank);
		*pAck << pBattle->GetData().nickname.c_str();
		*pAck << static_cast<uint16_t>(pBattle->GetData().attack_point);
		*pAck << static_cast<uint16_t>(ep_this_game);
		*pAck << static_cast<uint16_t>(opp_ep_current);
		*pAck << static_cast<uint16_t>(pBattle->GetData().ep_max);
		*pAck << static_cast<uint32_t>(pBattle->GetData().userseq);

	}
	pAck->MakePacket(CMD_SC_PK_RESULT);
	pUser->Send(pAck);

	pUser->SetEnumState(User::EnumState::BATTLE_ROYAL_END);

	// 이벤트 로그 기록
	NGameLog::LaunchingEvent(pUser, 3);

	pUser->GetCharacterInfo().ep_current += my_point;
	if( pUser->GetCharacterInfo().ep_current > pUser->GetCharacterInfo().ep_max )
		pUser->GetCharacterInfo().ep_current = pUser->GetCharacterInfo().ep_max;
	else if( pUser->GetCharacterInfo().ep_current < 0 )
		pUser->GetCharacterInfo().ep_current = 0;

//	NProxy::FlushCharacterInfoSyn(pUser, pUser->GetCharacterInfo().characterseq, false);
	pUser->FlushCharacterInfoCache();

	// detail info create
	::Network::Packet * 	pDetailInfoPacket = pUser->GetSpace()->GetDungeonClearDetailInfoPacket();
	if(false == pSpace->GetEndBattleRoyal())
	{
		*pDetailInfoPacket << static_cast<uint8_t>(pSpace->GetBattleTree().size());
		std::for_each(pSpace->GetBattleTree().begin(), pSpace->GetBattleTree().end(), [&](NLogic::Space::BATTLETREE::value_type & pair) {
			NLogic::Battle * pBattle = pair.second;
			User * pLocalUser = pBattle->GetUser();

			int rank = 0;
			int ep_this_game = 0;
			if (NULL != pLocalUser)
			{
				rank = ::ProjectZ::NLogic::NFunction::GetPKSessionRank(pLocalUser->GetUserSeq(), pSpace);
				ep_this_game = NResource::Static::InstancePtr()->GetPointResource()->PKEvolvePoint(pLocalUser, rank);

				// 길드 포인트 반영
				NProxy::ApplyGuildPoint(pLocalUser, 3, rank);
			}

			*pDetailInfoPacket << pBattle->GetData().userseq;
			*pDetailInfoPacket << static_cast<uint8_t>(0);	// isfriend
			*pDetailInfoPacket << static_cast<uint16_t>(pBattle->GetData().attack_point);
			*pDetailInfoPacket << static_cast<uint16_t>(pBattle->GetData().kill_count);
			*pDetailInfoPacket << static_cast<uint16_t>(pBattle->GetData().dead_count);
			*pDetailInfoPacket << static_cast<uint8_t>(pBattle->GetData().first_kill_count);
			*pDetailInfoPacket << static_cast<uint16_t>(pBattle->GetData().back_attack);
			*pDetailInfoPacket << static_cast<uint16_t>(pBattle->GetData().all_kill_count);
			*pDetailInfoPacket << static_cast<uint16_t>(ep_this_game);// evolution point
		} );

		pDetailInfoPacket->MakePacket(CMD_SC_PK_BATTLE_DETAIL_INFO);

		pSpace->SetEndBattleRoyal(true);
	}

	pBattleRoyalBattle->GetData()._pkReward = true;

	// 일일 미션
	NLogic::DailyMission * pDailMission = pUser->GetDailyMission();
	if (NULL != pDailMission)
	{
		pDailMission->MissionComplete(NLogic::DailyMission::MissionType::MISSION4);
	}
}

} /*NCommand*/ } /*ProjectZ*/


#endif /* _PROJECTZ_NUSER_NCOMMAND_CS_PK_RESULT_H_ */
